﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace RubricaSentimentale.Model
{
    [DataContract]
    public class Agenda:INotifyPropertyChanged
    {
        [DataMember]
        public ObservableCollection<Persona> Persone;
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection<Persona> dlist;
        [DataMember]
        public ObservableCollection<Persona> DisplayList
        { 
            get{return dlist;}
            set {
                dlist = value;
                notify("DisplayList");
             } 
        }


        private void notify(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("DisplayList"));
        }

        public Agenda()
        {
            Persone = new ObservableCollection<Persona>();
            DisplayList = Persone;
        }

        /// <summary>
        /// Add a person to the agenda
        /// </summary>
        /// <param name="p">Person to add</param>
        public void AddPersona(Persona p)
        {
            if(!Persone.Contains(p))
                Persone.Add(p);
            notify("DisplayList");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nome">Name of the person</param>
        /// <param name="cognome">Surname of the person</param>
        public void AddPersona(string nome, string cognome)
        {
            AddPersona(new Persona(nome, cognome));
            notify("DisplayList");

        }

        /// <summary>
        /// Remove a person from the agenda
        /// </summary>
        /// <param name="p">Person to remove</param>
        public void RemovePersona(Persona p)
        {
            Persone.Remove(p);
            notify("DisplayList");

        }


        /// <summary>
        /// Search people that match the key
        /// </summary>
        /// <param name="key">keyword for searching people (Ex. "Name Surname")</param>
        public void Search(string key)
        {
            DisplayList = new ObservableCollection<Persona>();
            foreach (Persona p in Persone)
                if (p.Contains(key))
                    DisplayList.Add(p);
            notify("DisplayList");
        }


        
    }
}
